package com.lesson01;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//分析性能问题
public class Test10 {
    //普通方式调用
    public static void test1(){
        Person user = new Person();
        long start = System.currentTimeMillis();
        for(int i=0;i<1_000_000_000;i++){
            user.getName();
        }
        long end = System.currentTimeMillis();

        System.out.println("普通方式花费的时间为："+(end-start)+"毫秒");
        }
        //反射方式调用
        public static void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            Person user = new Person();
            Class c1 = user.getClass();
            Method getName = c1.getDeclaredMethod("getName",null);
            long start = System.currentTimeMillis();
            for(int i=0;i<1_000_000_000;i++){
                getName.invoke(user,null);
            }
            long end = System.currentTimeMillis();

            System.out.println("反射方式花费的时间为："+(end-start)+"毫秒");
        }
        //反射方式调用 关闭检测
        public static void test3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            Person user = new Person();
            Class c1 = user.getClass();
            Method getName = c1.getDeclaredMethod("getName",null);
            getName.setAccessible(true);
            long start = System.currentTimeMillis();
            for(int i=0;i<1_000_000_000;i++){
                getName.invoke(user,null);
            }
            long end = System.currentTimeMillis();

            System.out.println("关闭检测花费的时间为："+(end-start)+"毫秒");
        }

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
            test1();
            test2();
            test3();
    }
}
